package com.leetcode;

import ds.ListNode;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No02
 * @description
 * @date 2021/1/25 下午4:37
 */
public class No02 {
    public static void main(String[] args) {
        ListNode l11 = new ListNode(2);
        ListNode l12 = new ListNode(4);
        ListNode l13 = new ListNode(3);
        l11.next = l12;
        l12.next = l13;

        ListNode l21 = new ListNode(5);
        ListNode l22 = new ListNode(6);
        ListNode l23 = new ListNode(4);
        l21.next = l22;
        l22.next = l23;

        ListNode res = addTwoNumbers(l11,l21);
        printListNode(res);

    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(-1);
        ListNode node = res;
        int curry = 0;
        while (l1 != null || l2 != null || curry != 0){
            int t1 = l1 == null ? 0 : l1.val;
            int t2 = l2 == null ? 0 : l2.val;
            int sum = t1 + t2 + curry;
            ListNode t = new ListNode(sum % 10);
            node.next = t;
            node = t;
            curry = sum / 10;
            if(l1 != null){
                l1 = l1.next;
            }
            if(l2 != null){
                l2 = l2.next;
            }
        }
        return res.next;

    }

    public static void printListNode(ListNode l){
        if(l == null){
            System.out.println("empty listnode");
        }else{
            while (l != null){
                System.out.print(l.val);
                System.out.print( " ");
                l = l .next;
            }
        }
    }

}
